package com.zhenyu.test;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
 
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
 
public class BtnColor extends JFrame implements ActionListener {
    int count = 0;
    JFrame frm = this;
 
    JPanel pan = new JPanel();
 
    JButton btn = new JButton("变色");
            
    public BtnColor(String name) {
        super(name);
        btn.setBounds(20, 20, 100, 80);
        btn.addActionListener(this);
        pan.setLayout(null);
        pan.add(btn);
        frm.add(pan, BorderLayout.CENTER);
        frm.setBounds(100, 100, 300, 200);
        frm.setDefaultCloseOperation(EXIT_ON_CLOSE);
        frm.setVisible(true);
        btn.setBackground(Color.BLACK);
    }
 
    // 按钮监听器
    public void actionPerformed(ActionEvent e) {
        int color = (count++ % 3);
        switch (color) {
        case 0:
            btn.setBackground(Color.PINK);
            break;
        case 1:
            btn.setBackground(Color.orange);
            break;
        case 2:
            btn.setBackground(Color.GREEN);
            break;
        }
    }
 
    public static void main(String[] args) {
        new BtnColor("按钮变色");
    }
}